﻿<%@ WebHandler Language="C#" Class="ExchangeHandlerService" %>


using System;
using System.Web;

using System.Collections;
using System.Collections.Generic;
using System.Reflection;

using XJX.BusinessLayer;

using XJX.Page;

public class ExchangeHandlerService : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";

        string response = "";

        String methodName = context.Request["method"];
        Type type = this.GetType();
        MethodInfo method = type.GetMethod(methodName);


        if (method == null)
        {
            response = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "调用的方法不存在！").ToJson(); return;
        }

        try
        {
            BeforeInvoke(methodName);
            response = method.Invoke(this, null).ToString();

            //int a = 1;//传递带参数数的方法。为方法传递参数
            //int b = 2;
            //response = method.Invoke(this, new object[]{a,b}).ToString();
        }
        catch (Exception ex)
        {
            Hashtable result = new Hashtable();
            result["error"] = -1;
            result["message"] = ex.Message;
            result["stackTrace"] = ex.StackTrace;
            String json = XJX.BusinessLayer.JSON.Encode(result);
            context.Response.Clear();
            context.Response.Write(json);
        }
        finally
        {
            AfterInvoke(methodName);
            HttpContext.Current.Response.Write(response);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    //权限管理
    protected void BeforeInvoke(String methodName)
    {
        //Hashtable user = GetUser();
        //if (user.role == "admin" && methodName == "remove") throw .      
    }
    //日志管理
    protected void AfterInvoke(String methodName)
    {

    }


    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================

   
    public string Add_New_ExchangeTheme()
    {
        string Title = HttpContext.Current.Request["Title"];
        string Content = HttpContext.Current.Request["Content"];
        FamilyInfo fi = (FamilyInfo)HttpContext.Current.Session["FamilyInfo"];
        string UID = fi.UID;

       // var UID = HttpContext.Current.Request["UID"]; //测试林景弘学生
        
        int ReturnValue = 0;
        string ReturnFieldValue = "";

        //下面开始调用信息保存
        bool b = XJX.Page.Exchange.Add_New_ExchangeTheme(Title,Content,UID, out ReturnValue, out ReturnFieldValue);

        string result = "";
        switch (ReturnValue)
        {
            case 1:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "交流主题添加成功，它的编号是：" + ReturnFieldValue,1).ToJson();
                break;
            case 99:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "交流主题添加失败，请检查数据!",99).ToJson();
                break;
            case 100:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "不能添加相同交流主题!",100).ToJson();
                break;
                
        }
        return result;
    }

    //添加新评论
    public string Add_New_ExchangeEvalInfo()
    {
        string ParentID = HttpContext.Current.Request["ExchangeID"];
        string Content = HttpContext.Current.Request["Content"];
        
        FamilyInfo fi = (FamilyInfo)HttpContext.Current.Session["FamilyInfo"];
        string UID = fi.UID;

        int ReturnValue = 0;
        string ReturnFieldValue = "";

        //下面开始调用信息保存
        bool b = XJX.Page.Evaluate.Ev_Add_New_Evaluate("Exchange",ParentID, UID, Content, out ReturnValue, out ReturnFieldValue);

        string result = "";
        switch (ReturnValue)
        {
            case 1:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "回复成功，它的编号是：" + ReturnFieldValue, 1).ToJson();
                break;
            case 99:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "回复失败，请检查数据!", 99).ToJson();
                break;
            case 100:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "不能添加相同回复内容!", 100).ToJson();
                break;

        }
        return result;
    }

    //删除一条交流主题
    public string Del_One_ExchangeTheme_From_EXID()
    {
        string result;
        string EXID = HttpContext.Current.Request["ExchangeID"];
        string UID = HttpContext.Current.Request["UID"];

        bool b = XJX.Page.Exchange.Del_One_ExchangeTheme_From_EXID(EXID,UID);

        if (b)
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "删除一条主题 成功！" ).ToJson();
        }
        else
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "删除一条主题 错误(此主题下有回复)！").ToJson();
        }

        return result;
    }

    //删除一条评论信息
    public string Del_One_ExchangeEvalInfo_From_ID()
    {
        string result;
        string ID = HttpContext.Current.Request["EvaluateID"];
        string UID = HttpContext.Current.Request["UID"];

        bool b = XJX.Page.Evaluate.Ev_Del_One_ContentEvaluate_From_ID(ID,UID);

        if (b)
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "删除一条回复 成功！").ToJson();
        }
        else
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "删除一条回复 失败！").ToJson();
        }

        return result;
    }

    //为某个主题内容点赞（再次点击则取消）
    public string Add_New_Credit()
    {
        string result;

        string ParentID = HttpContext.Current.Request["ExchangeID"];

        FamilyInfo fi = (FamilyInfo)HttpContext.Current.Session["FamilyInfo"];
        string UID = fi.UID;

        bool b = XJX.Page.Evaluate.Ev_Add_New_Credit("Exchange", ParentID, UID);

        if (b)
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "谢谢参与！").ToJson();
        }
        else
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "参与 失败！").ToJson();
        }

        return result;    
    }
    
    //修改主题内容
    public string Modify_One_ExchangeTheme_Info()
    {
        string EXID = HttpContext.Current.Request["ExchangeID"];
        string UID = HttpContext.Current.Request["UID"];//普通家庭修改时，不需要指定此参数，通过Session传递。管理员修改时要指定此参数。
        string Title = HttpContext.Current.Request["Title"];
        string Content = HttpContext.Current.Request["Content"];

        FamilyInfo fi = (FamilyInfo)HttpContext.Current.Session["FamilyInfo"];
        if (fi != null)
        {
            UID = fi.UID;
        }

        string result;

        bool b = XJX.Page.Exchange.Modify_One_ExchangeTheme_Info(EXID,UID,Title,Content);

        if (b)
        {           
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "修改一条主题 成功！").ToJson();
        }
        else
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "修改一条主题 错误！").ToJson();
        }

        return result;
    }

    //得到一个交流主题的内容
    public string Get_One_ExchangeTheme_Info_From_EXID()
    {
        string EXID = HttpContext.Current.Request["ExchangeID"];
        
        return XJX.Page.Exchange.Get_One_ExchangeTheme_Info_From_EXID(EXID);
    }

    //得到某个家庭发表的所有交流主题标题列表
    public string Get_One_Family_All_ExchangeTheme_List()
    {
        FamilyInfo fi = (FamilyInfo)HttpContext.Current.Session["FamilyInfo"];
        string UID = fi.UID;
        //var UID = HttpContext.Current.Request["UID"]; //测试林景弘学生

        string Title = HttpContext.Current.Request["Title"];

        int PageIndex = Convert.ToInt32(HttpContext.Current.Request["PageIndex"]);
        int PageSize = Convert.ToInt32(HttpContext.Current.Request["PageSize"]);

        return XJX.Page.Exchange.Get_One_Family_All_ExchangeTheme_List(UID,Title,PageIndex,PageSize);
    }

    //某个家庭针对某主题发表的所有回复内容
    public string Get_One_Family_ExchangeEvalInfo_List_By_UID()
    {
        string ParentID = HttpContext.Current.Request["ExchangeID"];
        FamilyInfo fi = (FamilyInfo)HttpContext.Current.Session["FamilyInfo"];
        string UID = fi.UID;

        int PageIndex = Convert.ToInt32(HttpContext.Current.Request["PageIndex"]);
        int PageSize = Convert.ToInt32(HttpContext.Current.Request["PageSize"]);

        return XJX.Page.Evaluate.Ev_Get_One_Family_ContentEvaluate_By_UID("Exchange",ParentID,UID, PageIndex, PageSize);
    }

    //一般浏览用户或者管理员获取交流主题的标题列表
    public string Get_ExchangeTheme_List()
    {
        string Title = HttpContext.Current.Request["Title"];
        string IsAll = HttpContext.Current.Request["IsAll"];//传入"ALL"字串，或者"PART"字串。管理员用ALL，一般用户用PART

        int PageIndex = Convert.ToInt32(HttpContext.Current.Request["PageIndex"]);
        int PageSize = Convert.ToInt32(HttpContext.Current.Request["PageSize"]);

        return XJX.Page.Exchange.Get_ExchangeTheme_List(Title, IsAll, PageIndex, PageSize);
    }

    //一般用户或者管理员获取某主题回复内容列表
    public string Get_ExchangeEvalInfo_List()
    {
        string ParentID = HttpContext.Current.Request["ExchangeID"];

        string IsAll = HttpContext.Current.Request["IsAll"];//传入"ALL"字串，或者"PART"字串。管理员用ALL，一般用户用PART

        int PageIndex = Convert.ToInt32(HttpContext.Current.Request["PageIndex"]);
        int PageSize = Convert.ToInt32(HttpContext.Current.Request["PageSize"]);

        return XJX.Page.Evaluate.Ev_Search_ContentEvaluate_List("Exchange",ParentID, IsAll, PageIndex, PageSize);
    }

    //屏蔽或显示一条交流主题
    public string ShieldORDisp_One_ExchangeTheme_By_EXID()
    {
        int ReturnValue;
        string ReturnFieldValue;

        string EXID = HttpContext.Current.Request["ExchangeID"];

        string result;

        bool b = XJX.Page.Exchange.ShieldORDisp_One_ExchangeTheme_By_EXID(EXID, out ReturnValue,out ReturnFieldValue);

        if (b)
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "操作成功！当前记录状态是" + ReturnFieldValue).ToJson();
        }
        else
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "操作错误！当前记录状态是" + ReturnFieldValue).ToJson();
        }

        return result;

    }


    //置顶一条交流主题
    public string  StickORCannel_One_ExchangeTheme_By_EXID()
    {
        int ReturnValue;
        string ReturnFieldValue;

        string EXID = HttpContext.Current.Request["ExchangeID"];

        string result;

        bool b = XJX.Page.Exchange.StickORCannel_One_ExchangeTheme_By_EXID(EXID, out ReturnValue, out ReturnFieldValue);

        if (b)
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "操作成功！当前记录状态是" + ReturnFieldValue).ToJson();
        }
        else
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "操作错误！当前记录状态是" + ReturnFieldValue).ToJson();
        }

        return result;
    }

    

}